package Servlet;

import Interface.User;
import SqlClass.UserData;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private class G
    {
       public Boolean IsHave=false;
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String resource = "SqlMapperConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=sqlSessionFactory.openSession(true);//拿到mybaits session对象,并关闭事务
        User map =session.getMapper(User.class);//拿到sql的映射接口
        UserData data=map.SelectUser(req.getParameter("user"));//获取数据
        G g=new G();
        if (data!=null)
        {
            g.IsHave=true;
        }else{
            System.out.println(req.getParameter("user")+req.getParameter("password"));
            map.InsertUser(req.getParameter("user"),req.getParameter("password"));
        }
        var JasData=JSON.toJSONString(g);

        resp.getWriter().write(JasData);
    }

}
